package com.cskaoyan.service.impl;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.wx.vo.TopicAndGoodsVo;
import com.cskaoyan.mapper.GoodsMapper;
import com.cskaoyan.mapper.TopicMapper;
import com.cskaoyan.service.TopicService;
import com.cskaoyan.utils.Constant;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * Description
 * 推广模块专题功能
 *
 * @author Zhangfei
 * @date Created on 2021/10/19
 */
@Service
@Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class TopicServiceImpl implements TopicService {
    @Autowired
    TopicMapper topicMapper;

    @Autowired
    GoodsMapper goodsMapper;

    /**
     * 通过分页条件查询专题
     *
     * @param title
     * @param subtitle
     * @param param
     * @return
     */
    @Override
    public BasePageData list(String title, String subtitle, BaseParam param) {
        //配置分页信息
        PageHelper.startPage(param.getPage(), param.getLimit());

        //构造查询条件
        TopicExample example = new TopicExample();
        //根据参数判断是否为null，不为null则拼接条件
        TopicExample.Criteria criteria = example.createCriteria();
        if (title != null && !"".equals(title)) {
            criteria.andTitleLike("%" + title + "%");
        }
        if (subtitle != null && !"".equals(subtitle)) {
            criteria.andSubtitleLike("%" + subtitle + "%");
        }
        criteria.andDeletedEqualTo(false);

        //添加排序条件
        example.setOrderByClause(param.getSort() + " " + param.getOrder());
        //查询
        List<Topic> topics = topicMapper.selectByExample(example);
        return BasePageData.ok(topics);
    }

    /**
     *
     * 通过专题id查询指定专题
     *
     * @param id
     * @return
     */
    @Override
    public TopicAndGoodsVo read(Integer id) {
        //通过id查询专题
        Topic topic = topicMapper.selectByPrimaryKey(id);
        //获取商品列表
        Integer[] goods = topic.getGoods();
        GoodsExample example = new GoodsExample();
        GoodsExample.Criteria criteria = example.createCriteria();
        List<Goods> goodsList = null;
        if(goods!=null&&goods.length!=0){
            criteria.andIdIn(Arrays.asList(goods));
            goodsList = goodsMapper.selectByExample(example);
        }

        //返回值
        TopicAndGoodsVo topicAndGoodsVo = new TopicAndGoodsVo();
        topicAndGoodsVo.setTopic(topic);
        topicAndGoodsVo.setGoodsList(goodsList);
        return topicAndGoodsVo;
    }

    /**
     * 修改专题
     *
     * @param topic
     * @return
     */
    @Override
    public Topic update(Topic topic) {
        //设置更新时间
        topic.setUpdateTime(new Date());
        //修改并响应修改后的数据
        topicMapper.updateByPrimaryKeySelective(topic);
        Topic topicToReturn = topicMapper.selectByPrimaryKey(topic.getId());
        return topicToReturn;
    }


    /**
     * 删除专题
     *
     * @param topic
     * @return
     */
    @Override
    public Integer delete(Topic topic) {
        //设置删除时间
        topic.setUpdateTime(new Date());
        topic.setDeleted(true);
        //执行
        return topicMapper.updateByPrimaryKeySelective(topic);
    }


    /**
     * 插入主题
     * @param topic
     * @return
     */
    @Override
    public Topic create(Topic topic) {
        //设置插入以及更新时间
        topic.setAddTime(new Date());
        topic.setUpdateTime(new Date());

        Integer count = topicMapper.insertSelective(topic);
        //根据返回的主键查询专题再响应回去
        Topic topicToReturn = null;
        if (count != 0) {
            topicToReturn = topicMapper.selectByPrimaryKey(topic.getId());
        }
        return topicToReturn;
    }
    @Override
    public BasePageData listInWechat(BaseParam param) {
        param.setSort(Constant.SORT);
        param.setOrder(Constant.ORDER);
        //配置分页信息
        PageHelper.startPage(param.getPage(), param.getLimit());
        //构造查询条件
        TopicExample example = new TopicExample();
        //根据参数判断是否为null，不为null则拼接条件
        TopicExample.Criteria criteria = example.createCriteria();
        criteria.andDeletedEqualTo(false);
        //添加排序条件
        example.setOrderByClause(param.getSort() + " " + param.getOrder());
        //查询
        List<Topic> topics = topicMapper.selectByExample(example);
        return BasePageData.ok(topics);
    }
    @Override
    public BasePageData relatedInWechat(BaseParam param,Integer id) {
        param.setSort(Constant.SORT);
        param.setOrder(Constant.ORDER);
        //配置分页信息
        PageHelper.startPage(param.getPage(), param.getLimit());
        //构造查询条件
        TopicExample example = new TopicExample();
        //根据参数判断是否为null，不为null则拼接条件
        TopicExample.Criteria criteria = example.createCriteria();
        criteria.andIdNotEqualTo(id);
        criteria.andDeletedEqualTo(false);
        //添加排序条件
        example.setOrderByClause(param.getSort() + " " + param.getOrder());
        //查询
        List<Topic> topics = topicMapper.selectByExample(example);
        return BasePageData.ok(topics);
    }
}
